<?


Class CommentModule{
    var $_connect;
    var $error;
    function CommentModule() {
        $this->_connect = new Dataconnection();
    }
    function getComment($_type,$id){
        $SQLCommand="
        select * 
        from `_comment` where `delete` is null and `_type`='$_type' and `id`='$id'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            $result=$data->fetch_array();
            return $result;
        }
        return null;
    }
    function getComments($_type,$parent){
        $SQLCommand="
        select * 
        from `_comment` where `delete` is null and `_type`='$_type' and `parent`='$parent'
        order by `insert` DESC
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function insertComment($_type,$name,$comment,$rate,$parent,$cmfor){
        //$name=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $name);
        //$brief=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $brief);
        //$des=  EndcodeStr($des);//str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $des);
        $acc=isset($_SESSION["accountID"])?"'".$_SESSION["accountID"]."'":"null";
        $SQLCommand="
        INSERT 
            `_comment`(
                        `name`,`content`,`_type`,`insert`,`account`,`rate`,`parent`,`cmfor`
            )
            VALUE(
                        '$name','$comment','$_type',now(),$acc,'$rate','$parent','$cmfor'
            )
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            $this->rateItem($cmfor,$rate);
            return true;
        }
        return false;
    }
    function updateComment($id,$comment){
        //$name=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $name);
        //$brief=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $brief);
        //$des=  EndcodeStr($des);//str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $des);
        $SQLCommand="
        UPDATE 
            `_comment`
        SET `content`='$comment',`update`=now()
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function deleteComment($id){
        //$name=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $name);
        //$brief=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $brief);
        //$des=  EndcodeStr($des);//str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $des);
        $SQLCommand="
        UPDATE 
            `_comment`
        SET `delete`=now()
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function rateItem($id,$rate){
        //$name=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $name);
        //$brief=  str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $brief);
        //$des=  EndcodeStr($des);//str_replace("'", htmlentities("'",ENT_QUOTES,'utf-8'), $des);
        $SQLCommand="
        UPDATE 
            `_template`
        SET `starr`=(`starr`*`viewer`+$rate)/(`viewer`+1),`viewer`=`viewer`+1
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
}
?>